#ifndef SBOTFOCPLUGIN_H
#define SBOTFOCPLUGIN_H

#include "./Lib/sbot_protocol.h"
#include "SbotFocChart.h"
#include "SbotPlugin.h"

#include <QObject>
#include <QQmlComponent>
#include <QQmlContext>
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>

#define PLUGIN_NAME "SbotFoc"
#define PLUGIN_IID "org.SBOT.SbotPlugin"
#define PLUGIN_QMLRES "qrc:/Qml/SbotFocPlugin.qml"

class SbotFocPlugin : public SbotPlugin {
    Q_OBJECT

    Q_PLUGIN_METADATA(IID PLUGIN_IID FILE "plugin.json")
    Q_INTERFACES(SbotPlugin)

  public:
    int flushFreqData = 5;   //定时器频率
    uint16_t timestamp = 0;  //时间戳

  private:
    QQmlComponent* pluginUi = nullptr;
    // QObject* qmlObj = nullptr;
    QSerialPort devPort;
    QTimer timerSendToUI;         // UI数据刷新定时器
    QByteArray rxDataBuffer;      // 数据接收缓冲区
    QMutex mutexRxbuffer;         //接收缓冲区互斥锁
    bool sfPacketIsNew = false;   //消息报文标志
    sp_packet_t sbot_foc_packet;  // sbot协议消息包
    QVector<QVariant> chartData;  //视图数据缓冲区

  public:
    explicit SbotFocPlugin(QObject* parent = nullptr);
    void PluginInitAtThreadHook() override;
    int PluginThreadLoop() override;
    Q_INVOKABLE void refreshDev(void);           // 刷新设备
    Q_INVOKABLE void openDev(QString portName);  //打开设备
    Q_INVOKABLE void terminalInput(const QString& text);

  private:
    void sendDataToui();      // 发送数据到UI
    void doDataReciveWork();  // 接收数据
    void processRawData();    //处理原始数据

  signals:
    void flushViewData(QVector<QVariant> chartData);  //刷新数据
};

#endif  // TEMINALPLUGIN_H
